package com.shitou.shitoucart.listener;

import com.shitou.shitoucart.config.RabbitMQConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.util.Date;

/**
 * class detail:
 *
 * @author 石头人
 */
@SuppressWarnings("all")
@Component
@Slf4j
public class AddCartListener {


    @RabbitListener(queues = {RabbitMQConfig.ADDCART_QUEUE})
    public void test(String msg, Channel channel, Message message){
        try {
            // 关单
            System.out.println(msg + "   我是一个");
            System.out.println(new Date());
            // 手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            // 消费失败后重新入队
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException e1) {
                log.error("释放库存失败,orderToken=[{}]", "sldfkjslkdjflgksjdfg");
            }
        }
    }

    @RabbitListener(queues = {RabbitMQConfig.TEST_QUEUE})
    public void test1(String msg, Channel channel, Message message){
        try {
            // 关单
            System.out.println(msg + "   我是两个");
            System.out.println(new Date());
            // 手动ACK
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            // 消费失败后重新入队
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
            } catch (IOException e1) {
                log.error("释放库存失败,orderToken=[{}]", "sldfkjslkdjflgksjdfg");
            }
        }
    }


}
